<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
var that
import * as echarts from "echarts";
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    PLdata:{
      type: Array,
      default: () => []
    },
    PSdata:{
      type: Array,
      default: () => []
    },
    rend: false
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick( () => {
      this.initChart()
      this.setOption(true)
    })
    that = this
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    rend() {
      this.setOption()
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
    },
    setOption(flag) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '8',
          data: this.PLdata
        },
        series: [
          {
            name: '',
            type: 'pie',
            // roseType: 'radius',
            radius: '80%',
            center: ['50%', '45%'],
            data: this.PSdata,
            label:{
              formatter: '{c}',
              position: 'inside'
            },
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
      if(flag) {
        this.chart.on('click',function handleClick(params){
          that.chart.off('click')
          that.$emit('handleSelectPolicy',params.data.id)
          setTimeout(()=>{that.chart.on('click',handleClick)},1200)
        })
      }
    }
  }
}
</script>
